package com.wk.springbootlearn.controller;

import com.wk.springbootlearn.common.model.Response;
import com.wk.springbootlearn.pojo.form.GetTeamForm;
import com.wk.springbootlearn.pojo.form.TeamForm;
import com.wk.springbootlearn.service.TeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@Api(value = "团队管理", tags = "团队管理模块")
@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private TeamService teamService;

    @ApiOperation("创建一个团队")
    @PostMapping("/create")
    public Response createTeam(@Valid @RequestBody TeamForm teamForm, @ApiIgnore @RequestAttribute("userId") String userId) {
        teamService.createTeam(teamForm, new Long(userId));
        return new Response().setOK();
    }

    @ApiOperation("修改一个团队")
    @PutMapping("/update/{id}")
    public Response updateTeam(@PathVariable("id") Long id, @Valid @RequestBody TeamForm teamForm, @ApiIgnore @RequestAttribute("userId") String userId) {
        teamService.updateTeam(id, teamForm, new Long(userId));
        return new Response().setOK();
    }

    @ApiOperation("分页查询团队列表")
    @PostMapping("/finds")
    public Response findTeam(@Valid @RequestBody(required = false) GetTeamForm getTeamForm) {
        if (getTeamForm == null) {
            return new Response().setData(teamService.findTeam(new GetTeamForm()));
        }
        return new Response().setData(teamService.findTeam(getTeamForm));
    }

    @ApiOperation("删除团队")
    @DeleteMapping("/del/{id}")
    public Response deleteTeam(@PathVariable("id") Long id) {
        teamService.delTeam(id);
        return new Response().setOK();
    }

}
